package com.softaria.server.seo;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.softaria.windows.model.PerspectiveUrl;
import com.softaria.windows.seo.HttpParametersConstants;

public class StaticContentTag extends SimpleTagSupport implements HttpParametersConstants {

  public static final String TYPE_CONTENT = "content";
  public static final String TYPE_KEYWORDS = "keywords";
  public static final String TYPE_TITLE = "title";
  public static final String TYPE_DESCRIPTION = "description";

  private String type = TYPE_CONTENT;

  public void doTag() throws JspException, IOException {
    PageContext pageContext = (PageContext) getJspContext();
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

    String contextPath = request.getContextPath();
    String servletPath = request.getServletPath();

    JspWriter jspWriter = pageContext.getOut();
    PerspectiveUrl url = buildPerspectiveUrl(request);

    if (TYPE_CONTENT.equals(type)) {
      jspWriter.write(buildStaticContent(servletPath, contextPath, url));
      return;
    }
    if (TYPE_DESCRIPTION.equals(type)) {
      String kw = getDescription(url);
      if (kw != null) {
        jspWriter.write("<meta name=\"description\" content=\"" + kw + "\"/>");
      }
      return;
    }
    if (TYPE_KEYWORDS.equals(type)) {
      String kw = getKeywords(url);
      if (kw != null) {
        jspWriter.write("<meta name=\"keywords\" content=\"" + kw + "\"/>");
      }
      return;
    }
    if (TYPE_TITLE.equals(type)) {
      jspWriter.write(getTitle(url));
      return;
    }

    jspWriter.write("Wrong type attribute=" + type);
  }

  protected PerspectiveUrl buildPerspectiveUrl(HttpServletRequest req) {

    PerspectiveUrl url = StaticPagesFactory.getInstance().getUrlBuilder().buildPerpspectiveUrl(
        new RequestParameterSet(req));

    if (url == null) {
      url = getDefaultUrl();
    }
    return url;
  }

  private PerspectiveUrl getDefaultUrl() {
    StaticMenu menu = StaticPagesFactory.getInstance().getMenu();
    if (menu != null) {
      return menu.getMenuConfiguration().getDefaultPerspective();
    } else {
      return null;
    }
  }

  private String buildStaticContent(String servletPath, String contextPath, PerspectiveUrl url) {
    String staticContent;

    String prefix = contextPath + servletPath + '/';

    if (url != null) {

      staticContent = retrieveStaticContent(url) + getMenuFooter(prefix, url);
    } else {
      staticContent = getMenuPage(prefix);
    }

    return staticContent;
  }

  private String retrieveStaticContent(PerspectiveUrl url) {
    StaticContenetRetriever contentRetriever = StaticPagesFactory.getInstance()
        .getContentRetriever();

    if (contentRetriever != null) {
      String ret = contentRetriever.retrieveStaticContent(url);

      return ret == null ? "" : ret;
    } else {
      return "";
    }
  }

  private String getKeywords(PerspectiveUrl url) {
    StaticContenetRetriever contentRetriever = StaticPagesFactory.getInstance()
        .getContentRetriever();

    if (contentRetriever != null) {
      String ret = contentRetriever.retrieveKeywords(url);

      return ret == null ? "" : ret;
    } else {
      return "";
    }
  }

  private String getDescription(PerspectiveUrl url) {
    StaticContenetRetriever contentRetriever = StaticPagesFactory.getInstance()
        .getContentRetriever();

    if (contentRetriever != null) {
      String ret = contentRetriever.retrieveDescription(url);

      return ret == null ? "" : ret;
    } else {
      return "";
    }
  }

  private String getTitle(PerspectiveUrl url) {
    StaticMenu menu = StaticPagesFactory.getInstance().getMenu();
    if (menu != null) {
      String title = menu.getMenuConfiguration().getMenuLabel(url);
      return title == null ? "" : title;
    } else {
      return "";
    }
  }

  private String getMenuPage(String prefix) {
    StaticMenu menu = StaticPagesFactory.getInstance().getMenu();

    if (menu != null) {
      return menu.getMenuPage(prefix);
    } else {
      return "";
    }
  }

  private String getMenuFooter(String prefix, PerspectiveUrl url) {
    StaticMenu menu = StaticPagesFactory.getInstance().getMenu();

    if (menu != null) {
      return menu.getMenuFooter(prefix, url);
    } else {
      return "";
    }

  }

  public void setType(String type) {
    this.type = type;
  }

}
